package com.platform.modules.quality.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.StringUtils;
import com.platform.dto.MessageSendDto;
import com.platform.modules.prj.entity.PrjWorkAreaEntity;
import com.platform.modules.prj.service.PrjWorkAreaService;
import com.platform.modules.quality.dao.QlyInspectionItemDao;
import com.platform.modules.quality.dao.QlyInspectionItemReformDao;
import com.platform.modules.quality.entity.QlyInspectionEntity;
import com.platform.modules.quality.entity.QlyInspectionItemEntity;
import com.platform.modules.quality.entity.QlyInspectionItemReformEntity;
import com.platform.modules.quality.service.QlyInspectionItemReformService;
import com.platform.modules.quality.service.QlyInspectionItemService;
import com.platform.modules.quality.service.QlyInspectionService;
import com.platform.service.MessageHandleService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class QlyInspectionItemReformServiceImpl extends ServiceImpl<QlyInspectionItemReformDao, QlyInspectionItemReformEntity> implements QlyInspectionItemReformService {

    private static final Logger logger = LoggerFactory.getLogger(QlyInspectionItemReformServiceImpl.class);
    
	@Autowired
	QlyInspectionItemService qlyInspectionItemService;
	@Autowired
	QlyInspectionService qlyInspectionService;
    @Autowired
    MessageHandleService messageHandleService;
    @Autowired
    PrjWorkAreaService prjWorkAreaService;

	@Override
	public Boolean isExist(QlyInspectionItemReformEntity entity) {
		List<QlyInspectionItemReformEntity> list =  baseMapper.existList(entity);
		if(null!=list && list.size()>0) {
			return true;
		}else {
			return false;
		}
	}

	@Override
	@Transactional
	public void saveAndUpdateInspectionStatus(QlyInspectionItemReformEntity reform) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String reformTime = df.format(new Date());// new Date()为获取当前系统时间
		reform.setReformTime(reformTime);
		baseMapper.insert(reform);
		//查询巡查记录 未提交整改的问题
		List<QlyInspectionItemEntity> list = qlyInspectionItemService.queryInspectionItemUnReformList(reform.getInsId(), reform.getInsItemId());
		//不存在未提交整改的问题
		if(null==list || list.size()<1) {
			//更新巡查记录状态为,待复查
			QlyInspectionEntity inspection = qlyInspectionService.getById(reform.getInsId());
			inspection.setQueStatus("2");
			inspection.setReformTime(reformTime);
			qlyInspectionService.updateById(inspection);
			//发送消息
			qlyInspectionService.sendMessage(inspection.getCreateUserId(), "ZLXC_DFC", inspection.getInsId());
		}		
	}

	@Override
	public void saveReviewAndUpdateInspectionStatus(QlyInspectionItemReformEntity reform, String userId) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String reviewTime = df.format(new Date());// new Date()为获取当前系统时间
		QlyInspectionItemReformEntity inspectionItemReform = baseMapper.selectById(reform.getInsItemReformId());
		if(null==inspectionItemReform) {
			throw new BusinessException("未找到整改记录");
		}
		inspectionItemReform.setReviewAdvise(reform.getReviewAdvise());
		inspectionItemReform.setReviewStatus(reform.getReviewStatus());
		if(StringUtil.isNotEmpty(reform.getReviewUserId())) {
			inspectionItemReform.setReviewUserId(reform.getReviewUserId());
		}else {
			inspectionItemReform.setReviewUserId(userId);
		}
		if(StringUtil.isNotEmpty(reform.getReviewTime())) {
			inspectionItemReform.setReviewTime(reform.getReviewTime());
		}else {
			inspectionItemReform.setReviewTime(reviewTime);
		}		
		baseMapper.updateById(inspectionItemReform);
		
		//如果复查结果是不合格, 更新巡查记录为待整改
		if("0".equals(reform.getReviewStatus())) {
			//更新巡查记录状态为,待整改
			QlyInspectionEntity inspection = qlyInspectionService.getById(reform.getInsId());
			inspection.setQueStatus("1");
			inspection.setReviewTime(reviewTime);
			qlyInspectionService.updateById(inspection);

			//发送消息
			String workAreaParentId = inspection.getWorkAreaParentId();
			if(StringUtil.isEmpty(workAreaParentId)) {
				logger.error("发送质量巡查-待复查 推送消息异常, 巡查记录ID:{}, 原因:工区不能为空",inspection.getInsId());
				return ;
			}
			PrjWorkAreaEntity prjWorkAreaEntity = prjWorkAreaService.getById(workAreaParentId);
			if(null==prjWorkAreaEntity) {
				logger.error("发送质量巡查-待复查 推送消息异常, 工区ID:{}, 原因:未找到工区",workAreaParentId);
				return ;
			}
			if(null==prjWorkAreaEntity.getResponsibleUserId()) {
				logger.error("发送质量巡查-待复查 推送消息异常, 工区ID:{}, 原因:未找到工区组负责人",workAreaParentId);
				return ;
			}
			qlyInspectionService.sendMessage(prjWorkAreaEntity.getResponsibleUserId(), "ZLXC_FCBHG", inspection.getInsId());
			
		//如果复查结果是合格, 判断是否所有问题都已经复查通过(通过则更新巡查记录为"已完成")
		}else {
			//查询巡查记录 整改复查通未通过的问题
			List<QlyInspectionItemEntity> list = qlyInspectionItemService.queryInspectionItemUnReviewList(reform.getInsId());
			//不存在 整改复查通未通过的问题
			if(null==list || list.size()<1) {
				//更新巡查记录状态为,已完成
				QlyInspectionEntity inspection = qlyInspectionService.getById(reform.getInsId());
				inspection.setQueStatus("3");
				inspection.setReviewTime(reviewTime);
				qlyInspectionService.updateById(inspection);

				//发送消息
				String workAreaParentId = inspection.getWorkAreaParentId();
				if(StringUtil.isEmpty(workAreaParentId)) {
					logger.error("发送质量巡查-待复查 推送消息异常, 巡查记录ID:{}, 原因:工区不能为空",inspection.getInsId());
					return ;
				}
				PrjWorkAreaEntity prjWorkAreaEntity = prjWorkAreaService.getById(workAreaParentId);
				if(null==prjWorkAreaEntity) {
					logger.error("发送质量巡查-待复查 推送消息异常, 工区ID:{}, 原因:未找到工区",workAreaParentId);
					return ;
				}
				if(null==prjWorkAreaEntity.getResponsibleUserId()) {
					logger.error("发送质量巡查-待复查 推送消息异常, 工区ID:{}, 原因:未找到工区组负责人",workAreaParentId);
					return ;
				}
				qlyInspectionService.sendMessage(prjWorkAreaEntity.getResponsibleUserId(), "ZLXC_FCHG", inspection.getInsId());
			}			
		}
	}
	
}

